package com.lebang.lechuangsystem.controller.sys;

import com.lebang.lechuangsystem.domain.Permission;
import com.lebang.lechuangsystem.model.PageBean;
import com.lebang.lechuangsystem.po.PermissionPo;
import com.lebang.lechuangsystem.service.sys.PermissionService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.common.UUIDUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Author: hwq
 * @Date: 2019/8/22 15:05
 * @Version 1.0
 */
@Api(tags = "<后台管理> 菜单管理")
@RestController
@RequestMapping("/sys/permission")
public class PermissionController {
    @Autowired
    private PermissionService baseService;

    @ApiOperation(value = "添加")
    @PostMapping("/add")
    public JsonData add(Permission vo) {
        vo.setCreatedTime(new Date());
        vo.setCode(UUIDUtil.L());
        vo.setHasDeleted(0);
        return baseService.insertModel(vo) > 0 ? Callback.success("200", "操作成功") : Callback.fail("200", "操作失败");
    }
    @ApiOperation(value = "修改")
    @PostMapping("/update")
    public JsonData update(Permission vo) {
        vo.setUpdatedTime(new Date());
        return baseService.updateById(vo) > 0 ? Callback.success("200", "操作成功") : Callback.fail("200", "操作失败");
    }
    @ApiOperation(value = "根据id获取详情")
    @GetMapping("/get/{id}")
    public JsonData get(@PathVariable Integer id, HttpServletRequest request) {
        return Callback.success("200", "查询成功", baseService.selectById(id));
    }

    @ApiOperation(value = "根据id删除")
    @PostMapping("/delete/{id}")
    public JsonData delete(@PathVariable Integer id, HttpServletRequest request) {
        return baseService.deleteById(id) > 0 ? Callback.success("200", "操作成功") : Callback.fail("200", "操作失败");
    }

    @ApiOperation(value = "分页")
    @PostMapping("/list")
    public JsonData list(HttpServletRequest request, @RequestParam Integer page, @RequestParam Integer limit,
                         @RequestParam(required = false) Integer status,
                         @RequestParam(required = false) String name) {
        PermissionPo example = new PermissionPo();
        example.setOffset((page - 1) * limit.longValue());
        example.setLimit(limit);
        PermissionPo.Criteria criteria = example.createCriteria();
        if(!TextUtil.isEmpty(status)){
            criteria.andEnabledEqualTo(status);
        }
        if(!TextUtil.isEmpty(name)){
            criteria.andNameLike("%"+name+"%");
        }
        example.setOrderByClause("sequence");
        PageBean<Permission> pageBean = baseService.pageByExample(example);
        return Callback.success("200", "查询成功", pageBean);
    }
    @ApiOperation(value = "获取所有一级菜单")
    @PostMapping("/list/parent")
    public JsonData getParent(HttpServletRequest request) {
        PermissionPo example = new PermissionPo();
        PermissionPo.Criteria criteria = example.createCriteria();
        criteria.andParentCodeEqualTo("0");
        example.setOrderByClause("sequence");
        PageBean<Permission> pageBean = baseService.pageByExample(example);
        Permission permission=new Permission();
        permission.setCode("0");
        permission.setName("无父级");
        permission.setParentCode("0");
        permission.setSequence(0);
        pageBean.getData().add(0,permission);
        return Callback.success("200", "查询成功", pageBean);
    }
}
